package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.StudentPageDTO;
import cn.kgc.vue.entity.Student;
import cn.kgc.vue.mapper.CourseMapper;
import cn.kgc.vue.mapper.StudentMapper;
import cn.kgc.vue.service.StudentService;
import cn.kgc.vue.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public ResponseResult getStudentPage(StudentPageDTO studentPageDTO) {

        ResponseResult responseResult = new ResponseResult();

        Page<Student> studentPage = new Page<>(studentPageDTO.getCurrentPage(), studentPageDTO.getLimit());
        LambdaQueryWrapper<Student> lambda = new QueryWrapper<Student>().lambda();
        lambda.eq(StringUtils.isNotBlank(studentPageDTO.getStudentNo()), Student::getStudentNo, studentPageDTO.getStudentNo())
                .eq(StringUtils.isNotBlank(studentPageDTO.getClazz()), Student::getClazz, studentPageDTO.getClazz())
                .orderByDesc(Student::getId);

        studentMapper.selectPage(studentPage, lambda);
        // 获取分页数据
        long total = studentPage.getTotal();
        List<Student> records = studentPage.getRecords();
        //  student  ->  studentvo
        List<StudentVO> studentVOs = records.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);
            Integer courseId = student.getCourseId();
            // 根据课程id  查询课程名称
            String courseName = courseMapper.selectById(courseId).getCourseName();
            studentVO.setCourseName(courseName);
            return studentVO;
        }).collect(Collectors.toList());

        //封装返回的数据
        HashMap<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("currentPageData", studentVOs);
        responseResult.success(ResponseStatus.SUCCESS, result);
        return responseResult;
    }

    @Override
    public ResponseResult batchAdd(MultipartFile file) throws Exception {

        ResponseResult responseResult = new ResponseResult();
        if (file.isEmpty()) {
            responseResult.fail();
            return responseResult;
        }

        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setTitleRows(1);


        List<Student> students = ExcelImportUtil.importExcel(inputStream, Student.class, params);
        // 将学生的数据  添加到数据库 判定学号是否重复
        for (Student student : students) {
            String studentNo = student.getStudentNo();
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("student_no", studentNo);
            Student stu = studentMapper.selectOne(studentQueryWrapper);
            if (ObjectUtil.isNotEmpty(stu)) {
                responseResult.fail(ResponseStatus.STUDENTNO_EXIST);
                return responseResult;
            }
            studentMapper.insert(student);
        }
        responseResult.success();
        return responseResult;
    }

    @Override
    public ResponseResult getScreenData() {
        ResponseResult responseResult = new ResponseResult();
        List<HashMap<String,Object>>  screenDatas =  studentMapper.selectScreenData();
        responseResult.success(ResponseStatus.SUCCESS,screenDatas);
        return responseResult;
    }
}




